package com.heima.wemedia;

import com.baidu.aip.util.Base64Util;
import com.heima.file.service.FileStorageService;
import com.heima.utils.baidu.HttpUtil;
import com.heima.utils.baidu.VerifyMaterial;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.net.URLEncoder;

/**
* 图像审核接口
*/
@SpringBootTest
public class ImgCensor {

    @Autowired
    private FileStorageService fileStorageService;


    @Test
    public void demo(){
        String demo = VerifyMaterial.VerifyText("曹操");
        System.out.println(demo);
    }

    @Test
    public void ImgCensor() {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined";
        try {
            // 本地文件路径
            byte[] images = fileStorageService.downLoadFile("http://192.168.200.130:9000/leadnews/2023/06/09/8c120b34e612479e979403dc79416fa9.jpg");
            String imgStr = Base64Util.encode(images);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            String param = "image=" + imgParam;
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = "24.3df1f7f4bd7035edf9b766440e9fa045.2592000.1688982609.282335-34642874";
            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}